'use strict';

const Controller = require('egg').Controller;


const createRule = {
  name: 'string'
}

class KanbanController extends Controller {

  /*
  * 查询所有
  * Method: get
  * path: /xxxx
  */
  async index() {
    const { ctx, service } = this

    let query = ctx.request.query
    const list = await ctx.service.kanban.index({user: ctx.session.id}, null, {
      sort: {'_id': -1}
    })

    ctx.helper.returnJson({
      ctx,
      data: list
    })

  }

  /*
  * 根据id查询
  * Method: get
  * path: /xxxx/:id
  */
  async show() {

    const { ctx, service } = this
    
    const res = await ctx.service.kanban.show({_id: ctx.params.id})

    res.createdTime = ctx.helper.formatTime(res.createdTime)
    res.upTime = ctx.helper.formatTime(res.upTime)
    
    ctx.helper.returnJson({
      ctx,
      data: res
    })
  }

  /*
  * 创建
  * Method: post
  * path: /xxxx
  */
  async create() {

    const { ctx, service } = this

    let body = ctx.request.body

    const res = await ctx.service.kanban.create({
      user: ctx.session.id,
      ...body
    })

    ctx.helper.returnJson({
      ctx,
      data: res
    })
  }

  /*
  * 修改更新
  * Method: put
  * path: /xxxx/:id
  */
  async update() {
    const { ctx, service } = this

    let body = ctx.request.body

    await ctx.service.kanban.update({_id: ctx.params.id }, {
      ...body,
      upTime: ctx.helper.formatTime(),
    })

    ctx.helper.returnJson({
      ctx,
      data: null
    })
  }

  /*
  * 删除
  * Method: DELETE
  * path: /xxxx/:id
  */
  async destroy() {
    const { ctx, service } = this

    await ctx.service.kanban.destroy({_id: ctx.params.id })

    ctx.helper.returnJson({
      ctx,
      data: null
    })
  }

  async addModule () {
    const { ctx, service } = this

    let body = ctx.request.body
    const res = await ctx.service.kanban.addModule(body.kanbanId, body.moduleList)
  
    ctx.helper.returnJson({
      ctx,
      data: res
    })
  }

  async removeModule () {
    const { ctx, service } = this

    let body = ctx.request.body
    const res = await ctx.service.kanban.removeModule(body.kanbanId, body.moduleId)
  
    ctx.helper.returnJson({
      ctx,
      data: res
    })
  }
}

module.exports = KanbanController
